package main

import (
	"context"
	"log"

	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/apache/rocketmq-client-go/v2/producer"
)

func main() {
	p, err := rocketmq.NewProducer(
		producer.WithNsResolver(primitive.NewPassthroughResolver([]string{"127.0.0.1:9876"})),
	)
	if err != nil {
		log.Fatalf("创建生产者失败: %s", err)
	}
	err = p.Start()
	if err != nil {
		log.Fatalf("启动生产者失败: %s", err)
	}
	defer p.Shutdown()

	// 发送 TagA 的消息
	msgA := &primitive.Message{
		Topic: "TagTopic",
		Body:  []byte("这是 TagA 的消息内容"),
	}
	msgA.WithTag("TagA")
	_, err = p.SendSync(context.Background(), msgA)
	if err != nil {
		log.Fatalf("发送 TagA 消息失败: %s", err)
	}

	// 发送 TagB 的消息
	msgB := &primitive.Message{
		Topic: "TagTopic",
		Body:  []byte("这是 TagB 的消息内容"),
	}
	msgB.WithTag("TagB")
	_, err = p.SendSync(context.Background(), msgB)
	if err != nil {
		log.Fatalf("发送 TagB 消息失败: %s", err)
	}

	log.Println("消息发送成功")
}
